<?php
    include "../database.php";
    
    
    $admin=$_POST["role"];
    $username=$_POST["username"];
    $name=$_POST["first_name"];
    $last_name=$_POST["last_name"];
    $email=$_POST["email"];
    $pass1=$_POST["password"];
    $pass2=$_POST["password_confirm"];
    
    //echo "<h1> $username, $name, $last_name, $email, $pass1, $pass2, $admin </h1>";
    $query = "SELECT * FROM user WHERE username='$username'";
    $result = executeQuery($query);
    
    $msg_id=-1;
    
    if(mysql_num_rows($result)==0){  //Preverjamo, koliko je ze uporabnikov s takim idjem, ce se ni nobenega, potem ga lahko dodamo
        if($pass1==$pass2){
            $query = "INSERT INTO `TPO15_2013`.`user` (`id`, `username`, `name`, `last_name`, `mail`, `pass`, `admin`) VALUES (NULL, '$username', '$name', '$last_name', '$email', '$pass1', '$admin')";
            executeQuery($query);
            
            $msg_id=200;
            //$_SESSION['success'] = "Uspesno dodan uporabnik $name $last_name!";
        }
        else{ 
            $msg_id=102;
            //$_SESSION['error'] = "Vneseni gesli se ne ujemata!";
        }
    }
    else{
        $msg_id=101;
        //$_SESSION['error'] = "To uporabnisko ime ze obstaja, izberite drugo!";
    }
    $get="";
    if($msg_id>0)
        $get="?msg=$msg_id";
    header("Location: ../control_panel.php$get");
    
?>

/* JS
    alert(form);
    //Uporabnika dodamo še pod vse uporabnike
    var user = queryStringToArray(form);
    var html = "<div id='a' class='project'>" +
                    "<div class='right'>|&nbsp;<a href='#' class='edit'>edit</a>&nbsp;•&nbsp;<a href='#' class='delete'>delete</a></div>"+
                    "<div class='left'><h4>"+ user['username']+ "</h4>, "+ user['mail'] +"> </div>"+
        "</div>"+

            "<div class='project_editing'>"+
            "<p id='error_msg' class='alert_string'></p>"+
            "<form method='post' class='editing_form' >"+
                    +"<input type='hidden' name='user_id' value='"+ user['user_id']+ "' /> <br/>"+
                    "<input type='text' name='username' placeholder='username' value='"+ user['username'] +"' required='required' /> <br/>"+
                    "<input type='text' name='name' placeholder='first name' value='"+ user['name'] +"' required='required' /> <br/>"+
                    "<input type='text' name='last_name' placeholder='last name' value='"+ user['last_name'] +"' required='required' /> <br/>"+
                    "<input type='email' name='mail' placeholder='e-mail' value='"+ user['mail'] +"' required='required' /> <br/>"+
                    "<div class='styled_select editing_select'>"+
                            "<select name='admin'> "+
                                    "<option value='' disabled>role</option>"+
                                    "<option value=1 selected>administrator</option>"+
                                    "<option value=0 >normal user</option>"+
                            "</select>"+
                    "</div>"+
                    "<input type='password' name='password' placeholder='password' required='required' />"+
                    "<input type='password' name='password_confirm' placeholder='confirm password' required='required' /> <br/>"+
                    "<button type='submit' class='edit_user'>confirm</button>"+
                    "<button type='button' class='right_button'>cancel</button>"+
            "</form>"+
            "</div>"+
            "<div class='project_deleting'>"+
                    "<button type='button' class='delete_user'>delete</button>"+
                    "<button type='button' class='right_button'>cancel</button>"+
            "</div>";
        alert(html);
        $("#all_users").append(html);

*/